import Primary from '../assets/json/primary'
import Middle from '../assets/json/middle'
import High from '../assets/json/high'

/**
 *  获取地址栏参数
 *  @param name
 *  @returns {String}
 */
export const getURLParams = (name) => {
  let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
  let r = window.location.search.substr(1).match(reg)
  if (r != null) return unescape(r[2])
  return null
}

/**
 *  获取随机证书数
 *  @param num 生成范围
 */
export const getRandom = (num) => {
  return Math.floor(Math.random() * num) + 1
}

/**
 *  获取随机题目题号
 *  @param type 题类型 1：初级 2：中级 3：高级
 */
export const getProNumList = (type) => {
  let list = []
  switch (type) {
    case 1:
      for (let i = 0; ; i++) {
        let rNum = getRandom(25)
        if (list.indexOf(rNum) === -1) {
          list.push(rNum)
          if (list.length === 8) {
            return list
          }
        }
      }
    case 2:
      for (let i = 0; ; i++) {
        let rNum = getRandom(27)
        if (list.indexOf(rNum) === -1) {
          list.push(rNum)
          if (list.length === 8) {
            return list
          }
        }
      }
    case 3:
      for (let i = 0; ; i++) {
        let rNum = getRandom(42)
        if (list.indexOf(rNum) === -1) {
          list.push(rNum)
          if (list.length === 8) {
            return list
          }
        }
      }
  }
}

/**
 *  获取题目列表
 *  @param type 题类型 1：初级 2：中级 3：高级
 */
export const getProList = (type) => {
  let numArr = getProNumList(type)
  let proJson = []
  let list = []
  switch (type) {
    case 1:
      proJson = Primary
      break
    case 2:
      proJson = Middle
      break
    case 3:
      proJson = High
      break
  }
  for (let i = 0; i < numArr.length; i++) {
    list.push(proJson[numArr[i] - 1])
  }
  return list
}
